<template>
  <div class="login">
    <HeaderComp />

    <el-card class="login-box">
      <h1 class="title">登录</h1>
      <el-form
        label-width="80"
        class="login-form"
        :model="loginForm"
        :rules="rules"
        ref="loginFormRef"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            placeholder="请输入用户"
            :prefix-icon="User"
            v-model="loginForm.username"
            clearable
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            placeholder="请输入密码"
            type="password"
            :prefix-icon="Lock"
            v-model="loginForm.password"
            clearable
            show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            style="width: 100%"
            @click="login"
            :loading="loading"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/tools'
import { useLoading } from '@/hooks/index'
import * as Regex from '@/utils/regexp'

const store = useStore()
const router = useRouter()
const { loading, setLoading } = useLoading()
const loginFormRef = ref()

const loginForm = reactive({
  username: 'Admin',
  password: '123456',
})

const validatorUserName = (rule, value, callback) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('用户名长度至少5位'))
  }
}

const validatorPassWord = (rule, value, callback) => {
  if (Regex.Password.test(value)) {
    callback()
  } else {
    callback(new Error('输入密码格式不正确'))
  }
}

const rules = {
  username: [
    { trigger: 'change', required: true, validator: validatorUserName },
  ],
  password: [
    { trigger: 'change', required: true, validator: validatorPassWord },
  ],
}

const login = async () => {
  try {
    await loginFormRef.value.validate()
    setLoading(true)
    await store.dispatch('user/login', loginForm)
    const { redirect, ...othersQuery } = router.currentRoute.value.query
    await router.push({
      path: redirect || '/',
      query: {
        ...othersQuery,
      },
    })
    ElNotification({
      type: 'success',
      title: `HI,${getTime()}好`,
      message: '登录成功',
    })
  } catch (e) {
    console.log(e)
  } finally {
    setLoading(false)
  }
}
</script>

<style scoped lang="scss">
.login {
  background: $color-bg-100;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  &-box {
    width: 440px;
    height: 280px;
    background: white;
    padding: 0 20px;

    .title {
      text-align: center;
    }

    .login-form {
      margin-top: 30px;
    }
  }

  .header {
    height: 60px;
    position: fixed;
    top: 0;
    left: 0;
  }
}
</style>
